# -*- coding: utf-8 -*-
import paho.mqtt.client as mqtt
import os
import time
import re
import subprocess
import shutil 
import hashlib
import threading
import smtplib
import  copy
import logging
from email import encoders
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.header import Header

from email.mime.multipart import MIMEMultipart
#publish_8910v1.3_at_liangjian@airm2m.com
#publish_1802S_at
#publish_8910v1.3_lua_bt_float
class  HandleEmail():
    def __init__(self,log):
        self.mail_host = "smtp.exmail.qq.com"
        self.sender = "bug@openluat.com"
        self.passwd = "Luat123456"
        self.logger = log
    
    def emailLog(self,mes):
        self.logger.info("email_log:%s"%mes)


    def addAttach(self,apath,filename='Report.html'):
        with open(apath, 'rb') as fp:
            attach = MIMEBase('application','octet-stream')
            attach.set_payload(fp.read())
            attach.add_header('Content-Disposition', 'attachment', filename=filename)
            encoders.encode_base64(attach)
            fp.close()
            return attach

    def SendTestEmail(self,email,data,att = False):
        self.emailLog("SendTestEmail %s "%email)
        receivers = [str(email, encoding = "gbk") ] 
        receiver = ','.join(receivers)

        msg = MIMEMultipart() 
        msg['From'] = self.sender 
        msg['To'] = receiver 
        msg['Subject'] = '测试结果' 
        
    
        msg.attach(MIMEText(data,_subtype='html', _charset='utf-8')) 
        

        try:
            smtpObj = smtplib.SMTP()
            smtpObj.connect(self.mail_host, 25)
            smtpObj.login(self.sender, self.passwd)
            smtpObj.sendmail(self.sender, receiver, msg.as_string())
            self.emailLog("send email sucess")
        except smtplib.SMTPException:
            self.emailLog("send email fail")

if __name__ == '__main__':
    pass
